package com.hexacta.hrs.sandbox.meetings.data;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hexacta.hrs.sandbox.meetings.model.Activity;
import com.hexacta.hrs.sandbox.meetings.model.Meeting;
import com.hexacta.hrs.sandbox.meetings.model.Mentor;
import com.hexacta.hrs.sandbox.meetings.model.Theme;

/**
 * Implementa la persistencia de la clase User.
 * @author ncortinez
 *
 */ 
public class MentorDao extends HibernateDaoSupport{

	public void saveOrUpdate(Object o) {
        this.getHibernateTemplate().saveOrUpdate(o);
        return;
    }
    
    public void delete(Mentor mentor) {
        this.getHibernateTemplate().delete(mentor);
    }
    
    public Mentor findById(long id) {
    	Mentor mentor = (Mentor) this.getHibernateTemplate().get(Mentor.class, Long.valueOf(id));
    	this.inicializar(mentor);
    	return mentor;
    }
    
    @SuppressWarnings("unchecked")
    public List<Mentor> findByName(String name) {
    	List<Mentor> mentors = this.getHibernateTemplate().find("from " + Mentor.class.getSimpleName() + " where name = '" + name + "'");
    	for (Mentor m : mentors) {
    		this.inicializar(m);
    	}
    	return mentors;
    }
    
    @SuppressWarnings("unchecked")
	public List<Mentor> findAll() {
    	List<Mentor> mentors = this.getHibernateTemplate().find("from " + Mentor.class.getSimpleName());
    	for (Mentor m : mentors) {
    		this.inicializar(m);
    	}
    	return mentors;
    }
    
    private void inicializar(Mentor mentor){
    	// FIXME no queremos recorrer todas las colecciones para cargar los datos. y si levantamos en eager?
			for (Meeting m2 : mentor.getMeetings()) {
				for (Theme t : m2.getThemes()) {
					for(Activity a : t.getActivities()){
						a.getId();
					}
				}
			}
    }    
}